'use strict';

angular.module('myApp.school', [])

.controller('schoolCtrl', ['$scope','$http','breadcrumbData',function($scope,$http,$breadcrumbData) {
    $breadcrumbData[3] = {name:"学校管理"};
    $scope.load = function(){
        $http.get('v1/school').success(function(data){
            $scope.schools = data.result['data'];
        });        
    }
    $scope.load();

    $scope.create = function(add){
        $http.post('v1/school',add ).success(function(data){
            if( !data.error ){
                alert("新增成功","success");
			    $scope.load();              
            }else{
                alert(data.message);
            }
        });
    };
    $scope.edit = function(school){
        $scope.add = school;
        $scope.isEdit = true;
    }
    $scope.edit_cancel = function(school){
        $scope.add = {};
        $scope.isEdit = false;
    }
    $scope.edit_submit = function(add){
        console.log(add);
        $http.put('v1/school/'+add.id,add).success(function(data){

            if( !data.error ){
                alert("修改成功","success");
                $scope.load(); 
                $scope.isEdit = false;  
                $scope.add = {}; 
            }else{
                alert(data.message);
            }
        });
    };

	$scope.delete = function(school){
	    $http.delete('v1/school/'+school.id).success(function(data){

			if( !data.error ){
				alert("删除成功","success");
                $scope.load();
			}else{
				alert(data.message);
			}
	    });
    };

}]);